<?php
/**
 * Created by PhpStorm.
 * User: jochen
 * Date: 2017/1/5
 * Time: 上午11:01
 */

namespace EWeiLink\JuPai;

use Jenssegers\Mongodb\Eloquent\Model;

/**
 * Class Planner
 * @package EWeiLink\JuPai
 *
 * @property string $id
 * @property string $user_id
 * @property string $name         // name of planner
 * @property string $phone        // number of planner
 * @property int    $plannerId    // planner id
 * @property string $branchInfo   // It where planner belong to
 * @property int    $validCode    // 特征码
 * @property string $password     // 用来让理财师通过认证的密码 （明文）
 * @method static Planner find($id)
 * @method static \Jenssegers\Mongodb\Eloquent\Builder byValidCode($content)
 */
class Planner extends Model
{
    protected $connection = Jupai::plugin_database_name;

    protected $guarded = [];

    protected $hidden = ['password', 'user_id', '_class'];

    static function new()
    {
        return new static;
    }

    /**
     * @param \Jenssegers\Mongodb\Eloquent\Builder $query
     * @param \App\Models\User|\EWeiLink\JuPai\User $user
     * @return \Jenssegers\Mongodb\Eloquent\Builder
     */
    public function scopeFindByUser($query, $user)
    {
        return $query->where(['user_id' => $user->id]);
    }

    /**
     * @param \Jenssegers\Mongodb\Eloquent\Builder $query
     * @param $validCode
     * @return \Jenssegers\Mongodb\Eloquent\Builder
     */
    public function scopeByValidCode($query, $validCode)
    {
        if (is_numeric($validCode)) {
            return $query->where('validCode', intval($validCode));
        }
        return $query;
    }

    public function contacts()
    {
        return $this->hasMany(Contact::class, 'plannerId', 'plannerId');
    }

    public function customers()
    {
        return $this->hasMany(JpCustomer::class, 'plannerId', 'plannerId');
    }


}
